package com.zadtek.shopmall.activities;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.BankCardItem;
import com.zadtek.shopmall.view.dialog.BaseNiceDialog;
import com.zadtek.shopmall.view.dialog.NiceDialog;
import com.zadtek.shopmall.view.dialog.ViewConvertListener;
import com.zadtek.shopmall.view.dialog.ViewHolder;

import java.util.LinkedList;
import java.util.List;

public class WithDrawActivity extends AbsActivity implements View.OnClickListener {
    private ImageView ivBack;
    private TextView topTitle;
    private TextView chooseCard,tvSubmit;
    private EditText etMoney;
    @Override
    public int setView() {
        return R.layout.activity_with_draw;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initLogic();
    }

    private void initLogic() {
        ivBack.setOnClickListener(this);
        chooseCard.setOnClickListener(this);
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        topTitle = (TextView) findViewById(R.id.title);
        topTitle.setText("提现申请");
        chooseCard = findViewById(R.id.choose_card);
        tvSubmit = findViewById(R.id.tv_submit);
        etMoney = findViewById(R.id.et_money);
    }

    private void pickcard() {
        final List<BankCardItem> payMode = new LinkedList<>();


        BankCardItem bankCardItem = new BankCardItem();
        bankCardItem.setChoose(true);
        payMode.add(bankCardItem);
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());
        payMode.add(new BankCardItem());

        final NiceDialog dialog = NiceDialog.init();
        dialog.setLayoutId(R.layout.base_choose_layout)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    protected void convertView(ViewHolder viewHolder, BaseNiceDialog baseNiceDialog) {
                        RecyclerView container = (RecyclerView) viewHolder.getView(R.id.recycleview);
                        ImageView close = (ImageView) viewHolder.getView(R.id.iv_close);
                        viewHolder.setText(R.id.choose_title,"选择银行卡");
                        close.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                            }
                        });
                        LinearLayoutManager layoutManager = new LinearLayoutManager(WithDrawActivity.this);
                        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                        container.setLayoutManager(layoutManager);
                        chooseAdapter adapter = new chooseAdapter(R.layout.bankcard_item,payMode);
                        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                dialog.dismiss();
                            }
                        });
                        container.setAdapter(adapter);
                    }
                }).setHeight(300)
                .setShowBottom(true);
        dialog.show(getSupportFragmentManager());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ic_back:
                finish();
                break;
            case R.id.choose_card:
                pickcard();
                break;
        }
    }

    private class chooseAdapter extends BaseQuickAdapter<BankCardItem,BaseViewHolder>{

        public chooseAdapter(int layoutResId, @Nullable List<BankCardItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, BankCardItem item) {
            if(item.isChoose()){
                Drawable drawable = getResources().getDrawable(R.drawable.choose);
                helper.setImageDrawable(R.id.iv_delete,drawable);
            }else{
                Drawable drawable = getResources().getDrawable(R.drawable.unchoose);
                helper.setImageDrawable(R.id.iv_delete,drawable);
            }
        }
    }
}
